import random

import requests
import re



# cookie_list = [
# 	{
# 		"session": "eyJjc3JmX3Rva2VuIjoiY1RubWd5YmVmYUF2UllEVUlCUFdRd0NPSmxrc1ppaE1xdFNqZHJ4RUhWS0ZOTG9HdVhweiIsInVzZXJuYW1lIjoiMSJ9.ZzGjsg.t51VGYUMukYv9E7TzwJD3h6vGh0"
# 	},
# 	{
# 		"session": "eyJjc3JmX3Rva2VuIjoiY3ZsUUxaVkZueUNhenJHV2lTcFhOWXhiZXF3c1JJRHVQSEJBRW1UdGZnb1VNaktrZGhKTyIsInVzZXJuYW1lIjoiMiJ9.ZzGj1g.vjD8CwJH0z9q1ujpmUJQAP_QaPU"
# 	},
# ]
# res = requests.get("https://quotes.toscrape.com/", cookies=random.choice(cookie_list))
#
# print(res.text)



# # 1. 获取token
# form_res = requests.get("https://quotes.toscrape.com/login")
# data = re.search(r'<input type="hidden" name=".*?" value="(.*?)"/>', form_res.text)
#
#
# # 2. 发起登录
# data = {
# 	"csrf_token": data.group(1),
# 	"username": "admin",
# 	"password": "1111111"
# }
# login_res = requests.post("https://quotes.toscrape.com/login", data=data, cookies=form_res.cookies)
# print(login_res.text)



session = requests.Session()
# 1. 发起get获取token
form_res = session.get("https://quotes.toscrape.com/login")
data = re.search(r'<input type="hidden" name=".*?" value="(.*?)"/>', form_res.text)
csrf_token = data.group(1)
print(form_res.cookies)
# 2. 发起第二次请求
data = {
	"csrf_token": data.group(1),
	"username": "admin",
	"password": "1111111"
}
login_res = session.post("https://quotes.toscrape.com/login", data=data)
print(login_res.text)
